//
//  PreferenceController.m
//
//  Created by Ryan on Thu Sep 25 2003.
//
//  ****************
//  Short Disclaimer (please read!):
//
//  Why open source?  This project was briefly popular several years ago and has since lost interest (on both
//  my part and the public's part).  Rather than letting it moulder for eternity, I decided to open source it 
//  in case anyone else is interested in renovating it and bringing it up to date.
//
//  My main concern with open sourcing it is that this program is one of the first Cocoa programs I wrote and
//  as such, it is in no way representative of my current coding style!  Many things are done incorrectly in 
//  this code base but I have not taken the time to revise them for the open source release. Hence, if you work
//  for a company looking to hire me, don't look too critically at this old code!
//
//  This code was originally written in 2002 to 2003 and hence was created before technologies such as 
//  Cocoa Bindings and properties (Obj-C 2.0) existed.  Much of the code could be ripped out and replaced 
//  with the newer way of doing things.
//
//  The GUI is also fairly out of date and in need of some serious updating.
//  
//  Have fun!
//  April, 2008.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.


#import "PreferenceController.h"

@interface PreferenceController ()

- (void)setupDefaultPreferences;

@end


@implementation PreferenceController

- (id)init {
	
    // Name of nib file to be loaded.
    if (self = [super initWithWindowNibName:@"Preferences"]) {
		[self setWindowFrameAutosaveName:@"PrefWindow"];
		
		if (! [[NSUserDefaults standardUserDefaults] boolForKey:@"Valid Prefs"]) {
			// This must be the first launch.
			[self setupDefaultPreferences];
		}
		
    }
    return self;
}


// This should be called if the program is launched without a valid preferences file.
- (void)setupDefaultPreferences {
	//NSLog(@"setting up default preferences");
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

	//NSLog(@"setting up default preferences");

	[defaults setBool:YES forKey:@"action on mouse click"];
	[defaults setBool:NO forKey:@"action on mouse over"];
	[defaults setBool:YES forKey:@"screen edge lock"];	
	[defaults setInteger:100 forKey:@"NumQueriesToRemember"];	
	[defaults setBool:YES forKey:@"FloatingWindow"];	
	[defaults setFloat: 98.0 forKey:@"transparency"];
	
	[self setShowsDockIcon: YES];
	
}


- (void)windowDidLoad {
	//NSLog(@"Preferences windowDidLoad");
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	// If this isn't the first launch, then set the prefs from the file.
	if ([defaults boolForKey:@"Valid Prefs"]) {

		// Restore the state.
		[mouseClickRadio setState:[defaults boolForKey:@"action on mouse click"]];
		[mouseOverRadio setState:[defaults boolForKey:@"action on mouse over"]];
		[edgeLockCheck setState:[defaults boolForKey:@"screen edge lock"]];
		[numQueriesToRememberField setIntValue: [defaults integerForKey:@"NumQueriesToRemember"]];
		[transparencySlider setFloatValue:[defaults floatForKey:@"transparency"]];	
		[floatingWindowCheck setState:[defaults boolForKey:@"FloatingWindow"]];
	} 	
	
	[showDockIconCheck setState:[self showsDockIcon]];
}


- (IBAction)showDockIconMessage:(id)sender {
	NSRunAlertPanel(@"Please Note",
				 @"For this change to take effect, you will have to quit Simple Search, move it to a different folder, and then move it back again.  This is due to caching within Mac OS X.",
				 NSLocalizedString(@"OK", nil), nil, nil);
}


// Is the Info.plist file set to show the dock icon and menubar?
- (BOOL)showsDockIcon {
	NSString * path = [NSString stringWithFormat: 
		@"%@/Contents/Info.plist", [[NSBundle mainBundle] bundlePath]];
	
	NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile: path];
	
	return ([[dict objectForKey:@"NSUIElement"] isEqualToString:@"0"]);
}


// Set whether or not Simple Search should show the dock icon and menu bar.
// note, this setting is in the "Info.plist" file which is located within
// the SimpleSearch.app bundle. Setting NSUIElement to 1 makes it invisible,
// setting it to 0 makes it visible.  However, you may have to move the application
// to another folder and back for this to take effect.
- (void)setShowsDockIcon:(BOOL)b {
	
	NSBundle * bund = [NSBundle mainBundle];
	NSString * path = [NSString stringWithFormat: 
		@"%@/Contents/Info.plist", [bund bundlePath]];
	
	NSMutableDictionary * dict = [NSDictionary dictionaryWithContentsOfFile: path];
	
	if (b) {
		[dict setObject:[NSString stringWithString:@"0"] forKey:@"NSUIElement"];
	} else {
		[dict setObject:[NSString stringWithString:@"1"] forKey:@"NSUIElement"];
	}
	
	BOOL status = [dict writeToFile:path atomically:YES];
	
	// Now, we have to move the application to another directory and then back before this will take effect.
	// For now, we'll just alert the user to this fact when they change the preferences
	// but eventually, it would be nice to do it automatically...
	//	[bund executablePath];
	
}



// If something happened...  record the preferences.
- (IBAction)prefsChanged:(id)sender {
	//NSLog(@"[PreferenceController prefsChanged] called");
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];	
	
	// Update the saved preferences.
		
	[defaults setBool:[mouseClickRadio state] forKey:@"action on mouse click"];
	[defaults setBool:[mouseOverRadio state] forKey:@"action on mouse over"];
	[defaults setBool:[edgeLockCheck state] forKey:@"screen edge lock"];
	[defaults setInteger:[numQueriesToRememberField intValue] forKey:@"NumQueriesToRemember"];	
	[defaults setFloat: [transparencySlider floatValue] forKey:@"transparency"];	
	[defaults setBool: [floatingWindowCheck state] forKey:@"FloatingWindow"];
	
	[self setShowsDockIcon: [showDockIconCheck state]];
	
	NSNotificationCenter *nc;
	nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MOOPreferencesUpdated" object:self];
}


- (void)windowWillClose:(NSNotification *)aNotification {
	[self prefsChanged:self];  // Update the prefs when they close the window.
}


-(void)dealloc {

	
	[super dealloc];
}


@end
